package com.libraries.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;


public class LetterIndexView extends View {
	
	private static final String[] ALPHABETS = {"#","A","B","C","D","E","F","G","H","I","J","K","L"
			,"M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
	
	private int choose = -1;
	private Context context;
	private Paint paint = new Paint();
	private boolean showBkg = false;
	private OnLetterChangedListener onChangedListener;

	public LetterIndexView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.context=context;
	}

	public LetterIndexView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context=context;
	}

	public LetterIndexView(Context context) {
		super(context);
		this.context=context;
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		if(showBkg){
		    canvas.drawColor(Color.parseColor("#40000000"));
		}
		
	    int height = getHeight();
	    int width = getWidth();
	    int singleHeight = height / ALPHABETS.length;
	    for(int i=0;i<ALPHABETS.length;i++){
	       paint.setColor(Color.parseColor("#00BFFF"));
	       paint.setTypeface(Typeface.DEFAULT_BOLD);
	       paint.setAntiAlias(true);
           paint.setTextSize(dp2px(context, 15)); 
	       if(i == choose){
	    	   paint.setColor(Color.parseColor("#3399ff"));
	    	   paint.setFakeBoldText(true);
	       }
	       float xPos = width/2  - paint.measureText(ALPHABETS[i])/2;
	       float yPos = singleHeight * i + singleHeight;
	       
	       canvas.drawText(ALPHABETS[i], xPos, yPos, paint);
	       paint.reset();
	    }
	   
	}
	
	public static int dp2px(Context context, float dpVal)
	{
		return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
				dpVal, context.getResources().getDisplayMetrics());
	}
	
	@Override
	public boolean dispatchTouchEvent(MotionEvent event) {
		final int action = event.getAction();
	    final float y = event.getY();
	    final int oldChoose = choose;
	    final OnLetterChangedListener listener = onChangedListener;
	    final int c = (int) (y/getHeight()*ALPHABETS.length);
	    
		switch (action) {
			case MotionEvent.ACTION_DOWN:
				showBkg = true;
				if(oldChoose != c && listener != null){
					if(c > 0 && c<ALPHABETS.length){
						listener.onLetterChanged(ALPHABETS[c]);
						choose = c;
						invalidate();
					}
				}
				
				break;
			case MotionEvent.ACTION_MOVE:
				if(oldChoose != c && listener != null){
					if(c > 0 && c<ALPHABETS.length){
						listener.onLetterChanged(ALPHABETS[c]);
						choose = c;
						invalidate();
					}
				}
				break;
			case MotionEvent.ACTION_UP:
				showBkg = false;
				choose = -1;
				invalidate();
				break;
		}
		return true;
	}

	public void setOnLetterChangedListener(
			OnLetterChangedListener onChangedListener) {
		this.onChangedListener = onChangedListener;
	}

	public interface OnLetterChangedListener{
		public void onLetterChanged(String letter);
	}
	
}
